package com.shangguigu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.shangguigu.common.Result;
import com.shangguigu.entity.Banner;
import com.shangguigu.service.BannerService;
import com.shangguigu.service.FileUploadService;
import com.shangguigu.service.impl.FileUploadServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

//@CrossOrigin
@Tag(name = "轮播图管理接口")
@RestController
@RequestMapping("/api/banners")
public class BannerController {
    @Autowired
     private  BannerService bannerService;
    @Autowired
    private FileUploadService fileUploadService;



    @Operation(summary = "获取所有轮播图信息")
    @GetMapping("/list")
    public Result list(){
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Banner> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //排序字段升序排序
         objectLambdaQueryWrapper.orderByAsc(Banner::getSortOrder);
         //调用bannerService中的list方法
        List<Banner> list = bannerService.list(objectLambdaQueryWrapper);
        return Result.success(list);
    }
    @Operation(summary = "获取激活状态的轮播图信息")
    @GetMapping("/active")
    public Result active(){
        List<Banner> list = bannerService.list(new LambdaQueryWrapper<Banner>().eq(Banner::getIsActive, true));
        return Result.success(list);
    }
    @Operation(summary = "根据id获取轮播图信息")
    @GetMapping("/api/banners/{id}")
    public Result<Banner> getById(@PathVariable Long id){
        Banner byId = bannerService.getById(id);
        return Result.success(byId);
    }
    @Operation(summary = "添加轮播图信息")
    @PostMapping("/add")
    public Result add(@RequestBody Banner banner){
        boolean save = bannerService.save(banner);
        if (save){
            return Result.success("添加成功");
        }else {
            return Result.success("添加失败");
        }
    }

    @RequestMapping(method = {RequestMethod.POST,RequestMethod.PUT})
    @Operation(summary = "更新轮播图信息")
    @PutMapping("/update")
    public Result update(@RequestBody Banner banner){
        boolean b = bannerService.updateById(banner);
        if (b){
            return Result.success("更新成功");
        }else {
            return Result.success("更新失败");
        }
    }
    @Operation(summary = "更新id删除轮播图")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        boolean b = bannerService.removeById(id);
            if (b){
                return Result.success("删除成功");
        }else {
                return  Result.success("删除失败");
        }
    }
    @Operation(summary = "上传轮播图")
    @PostMapping("/upload-image")
    public Result uploadImage(MultipartFile file){
        String url = fileUploadService.uploadFile(file);
        return  Result.success(url,"上传成功");
    }


}
